<?php
namespace System\Controller;
use Common\Controller\AdminbaseController;
class SettingController extends AdminbaseController{

	protected $options_model;

	public function _initialize() {
		parent::_initialize();
		$this->options_model = D("Common/SystemOptions");
	}

	//选项管理
	public function option(){
		$option = M('school')->where("id = '1'")->find();
		if(IS_POST){
			$data['school'] = I('school');
			$data['education'] = I('education');
			$save = M('school')->where("id = '1'")->save($data);
			if ($save) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}
		}
		$this->assign('option',$option);
		$this->display();
	}
	
	//公司信息
	public function company(){
		$company =M('system_company')->where("id = '1'")->find();
		if(IS_POST){
			$data['addr'] = I('addr');
			$data['tel'] = I('tel');
			$save = M('system_company')->where("id = '1'")->save($data);
			if ($save) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}
		}
		$this->assign('company',$company);
		$this->display();
	}
	//成绩查询网址
	public function select(){
		$company =M('moreurl')->order("id asc")->select();
		$num = M('moreurl')->count();
		if(IS_POST){
			$m = M('moreurl');
			foreach($_POST as $k => $v){
				if(substr($k,0,7) == 'urlname'){
					$id = substr($k,7);
					$is = $m->field('id')->where("id = '$id'")->find();
					$name_data['name'] = $v;
					if($is){
						$name_save = $m->where("id = '$id'")->save($name_data);
					}else{
						$name_add = $m->add($name_data);
					}
					if($name_save || $name_add){
						$return = 1;
					}
				}else{
					$id = substr($k,3);
					if($name_add){
						$id = $name_add;
					}
					$is = $m->field('id')->where("id = '$id'")->find();
					$url_data['url'] = $v;
					if($is){
						$url_save = $m->where("id = '$id'")->save($url_data);
					}else{
						$name_add = $m->add($url_data);
					}
					if($url_save || $name_add){
						$return = 1;
					}
				}
			}
			if ($return == 1) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}
		}
		$this->assign('num',$num);
		$this->assign('company',$company);
		$this->display();
	}
	public function del(){
		$id = I('id');
		$del = M('moreurl')->where("id = '$id'")->delete();
		if($del){
			$this->success('删除成功！');
		}else{
			$this->error('删除失败！');
		}
	}
	// 网站信息
	public function site(){
		C(S('sp_dynamic_config'));//加载动态配置
		$option=$this->options_model->where("option_name='site_options'")->find();
		$cmf_settings=$this->options_model->where("option_name='cmf_settings'")->getField("option_value");
		$tpls=sp_scan_dir(C("SP_TMPL_PATH")."*",GLOB_ONLYDIR);
		$noneed=array(".","..",".svn");
		$tpls=array_diff($tpls, $noneed);
		$this->assign("templates",$tpls);

		$adminstyles=sp_scan_dir("public/simpleboot/themes/*",GLOB_ONLYDIR);
		$adminstyles=array_diff($adminstyles, $noneed);
		$this->assign("adminstyles",$adminstyles);
		if($option){
			$this->assign(json_decode($option['option_value'],true));
			$this->assign("option_id",$option['option_id']);
		}
		$cdn_settings=sp_get_option('cdn_settings');
		$this->assign("cdn_settings",$cdn_settings);
		$this->assign("cmf_settings",json_decode($cmf_settings,true));

		$this->display('site');
	}
	public  function  options_list(){
		$option=$this->options_model->where("option_name='site_options'")->find();
		echo json_encode($option);
	}
	// 网站信息-seo
	public function seo(){
		C(S('sp_dynamic_config'));//加载动态配置
		$option=$this->options_model->where("option_name='site_options'")->find();
		$cmf_settings=$this->options_model->where("option_name='cmf_settings'")->getField("option_value");
		$tpls=sp_scan_dir(C("SP_TMPL_PATH")."*",GLOB_ONLYDIR);
		$noneed=array(".","..",".svn");
		$tpls=array_diff($tpls, $noneed);
		$this->assign("templates",$tpls);

		$adminstyles=sp_scan_dir("public/simpleboot/themes/*",GLOB_ONLYDIR);
		$adminstyles=array_diff($adminstyles, $noneed);
		$this->assign("adminstyles",$adminstyles);
		if($option){
			$this->assign(json_decode($option['option_value'],true));
			$this->assign("option_id",$option['option_id']);
		}

		$cdn_settings=sp_get_option('cdn_settings');

		$this->assign("cdn_settings",$cdn_settings);

		$this->assign("cmf_settings",json_decode($cmf_settings,true));

		$this->display('seo');
	}

	// 网站信息-cdn
	public function cdn(){
		C(S('sp_dynamic_config'));//加载动态配置
		$option=$this->options_model->where("option_name='site_options'")->find();
		$cmf_settings=$this->options_model->where("option_name='cmf_settings'")->getField("option_value");
		$tpls=sp_scan_dir(C("SP_TMPL_PATH")."*",GLOB_ONLYDIR);
		$noneed=array(".","..",".svn");
		$tpls=array_diff($tpls, $noneed);
		$this->assign("templates",$tpls);

		$adminstyles=sp_scan_dir("public/simpleboot/themes/*",GLOB_ONLYDIR);
		$adminstyles=array_diff($adminstyles, $noneed);
		$this->assign("adminstyles",$adminstyles);
		if($option){
			$this->assign(json_decode($option['option_value'],true));
			$this->assign("option_id",$option['option_id']);
		}

		$cdn_settings=sp_get_option('cdn_settings');

		$this->assign("cdn_settings",$cdn_settings);

		$this->assign("cmf_settings",json_decode($cmf_settings,true));

		$this->display('cdn');
	}


	// 网站信息设置提交
	public function site_post(){
		if (IS_POST) {
			if(isset($_POST['option_id'])){
				$data['option_id']=I('post.option_id',0,'intval');
			}
			$options=I('post.options/a');

			$configs["SP_SITE_ADMIN_URL_PASSWORD"]=empty($options['site_admin_url_password'])?"":md5(md5(C("AUTHCODE").$options['site_admin_url_password']));
			$configs["SP_DEFAULT_THEME"]=$options['site_tpl'];
			$configs["DEFAULT_THEME"]=$options['site_tpl'];
			$configs["SP_ADMIN_STYLE"]=$options['site_adminstyle'];
			$configs["URL_MODEL"]=$options['urlmode'];
			$configs["URL_HTML_SUFFIX"]=$options['html_suffix'];
			$configs["COMMENT_NEED_CHECK"]=empty($options['comment_need_check'])?0:1;
			$comment_time_interval=intval($options['comment_time_interval']);
			$configs["COMMENT_TIME_INTERVAL"]=$comment_time_interval;
			$_POST['options']['comment_time_interval']=$comment_time_interval;
			$configs["MOBILE_TPL_ENABLED"]=empty($options['mobile_tpl_enabled'])?0:1;
			$configs["HTML_CACHE_ON"]=empty($options['html_cache_on'])?false:true;

			sp_set_dynamic_config($configs);//sae use same function

			$data['option_name']="site_options";
			$data['option_value']=json_encode($options);
			if($this->options_model->where("option_name='site_options'")->find()){
				$result=$this->options_model->where("option_name='site_options'")->save($data);
			}else{
				$result=$this->options_model->add($data);
			}

			$cmf_settings=I('post.cmf_settings/a');
			$banned_usernames=preg_replace("/[^0-9A-Za-z_\x{4e00}-\x{9fa5}-]/u", ",", $cmf_settings['banned_usernames']);
			$cmf_settings['banned_usernames']=$banned_usernames;

			sp_set_cmf_setting($cmf_settings);

			$cdn_settings=I('post.cdn_settings/a');
			sp_set_option('cdn_settings', $cdn_settings);

			if ($result!==false) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}

		}
	}


	// 上传限制设置界面
	public function upload(){
		$upload_setting=sp_get_upload_setting();
		$this->assign($upload_setting);
		$this->display();
	}

	// 上传限制设置界面提交
	public function upload_post(){
		if(IS_POST){
			$result=sp_set_option('upload_setting',I('post.'));
			if($result!==false){
				$this->success('保存成功！');
			}else{
				$this->error('保存失败！');
			}
		}

	}

	// 清除缓存
	public function clearcache(){
		sp_clear_cache();
		$this->display();
	}


}